﻿using EFAPIProject.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;


namespace EFAPIProject.Data.Configurations
{
    public class UserConfiguration : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            // 表名
            builder.ToTable("Users");

            // 主键
            builder.HasKey(u => u.Id);

            // 自增列
            builder.Property(u => u.Id).ValueGeneratedOnAdd();

            // UserName：非空，最大长度50
            builder.Property(u => u.UserName)
                .IsRequired()
                .HasMaxLength(50);

            // Email：非空，唯一索引
            builder.Property(u => u.Email)
                .IsRequired()
                .HasMaxLength(100);
            builder.HasIndex(u => u.Email).IsUnique();

            // CreateTime：默认值为当前时间（SQL Server 函数）
            builder.Property(u => u.CreateTime)
                .HasDefaultValueSql("GETDATE()");
        }
    }
}
